/**
 * 
 */
package cwinsor.com.leaf_raking;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author cwinsor
 *
 */
public class Supervisor {

	/*
	 * properties
	 */
	String field0;
	String first;
	String last;

	Boolean avail2am;
	Boolean avail2pm;
	Boolean avail3am;
	Boolean avail3pm;
	Boolean avail9am;
	Boolean avail9pm;
	Boolean avail10am;
	Boolean avail10pm;
	Team assign2am;
	Team assign2pm;
	Team assign3am;
	Team assign3pm;
	Team assign9am;
	Team assign9pm;
	Team assign10am;
	Team assign10pm;
	String phoneC1;
	String phoneC2;
	
	/*
	 * debug
	 */
	private int debugSection = 3;
	String dM = this.getClass().getSimpleName() + "----";
	String dF = this.getClass().getSimpleName() + "---- ERROR ---- ";

	/*
	 * addData
	 * take in raw data, check, and internalize
	 */
	public void addData(ArrayList<String> theFieldsObject) {

		// sanity check - first field should be "supervisor"
		field0 = theFieldsObject.get(0);
		if (!field0.equals("supervisor")) {
			System.out.println(dF + "first field should be supervisor but is "
					+ field0.toString());
			System.exit(1);
		}

		first = theFieldsObject.get(1);

		last = theFieldsObject.get(2);

		if (debugSection==3) {
			System.out.println(dM + "adding ---> " + first + " " + last);
		}
		
		// availability
		avail2am = stringToBoolean(theFieldsObject.get(8));
		avail2pm = stringToBoolean(theFieldsObject.get(9));
		avail3am = stringToBoolean(theFieldsObject.get(10));
		avail3pm = stringToBoolean(theFieldsObject.get(11));

		avail9am = stringToBoolean(theFieldsObject.get(12));
		avail9pm = stringToBoolean(theFieldsObject.get(13));
		avail10am = stringToBoolean(theFieldsObject.get(14));
		avail10pm = stringToBoolean(theFieldsObject.get(15));

		// team assignments
		assign2am = Team.stringToTeam(theFieldsObject.get(18));
		assign2pm = Team.stringToTeam(theFieldsObject.get(19));
		assign3am = Team.stringToTeam(theFieldsObject.get(20));
		assign3pm = Team.stringToTeam(theFieldsObject.get(21));

		assign9am = Team.stringToTeam(theFieldsObject.get(22));
		assign9pm = Team.stringToTeam(theFieldsObject.get(23));
		assign10am = Team.stringToTeam(theFieldsObject.get(24));
		assign10pm = Team.stringToTeam(theFieldsObject.get(25));

		phoneC1 = theFieldsObject.get(30);
		phoneC2 = theFieldsObject.get(31);
	}


	public String getFirst() {
		return first;
	}

	public String getLast() {
		return last;
	}
	
	public String getPhoneC1() {
		return phoneC1;
	}
	public String getPhoneC2() {
		return phoneC2;
	}

	/*
	 * returns availability given and day and shift
	 */
	public Boolean getAvailability(RakerColumns rakerColumns, Shift shift) {

		if (rakerColumns.equals(RakerColumns.NOV_2) && shift.equals(Shift.AM)) { return avail2am; }
		if (rakerColumns.equals(RakerColumns.NOV_2) && shift.equals(Shift.PM)) { return avail2pm; }
		if (rakerColumns.equals(RakerColumns.NOV_3) && shift.equals(Shift.AM)) { return avail3am; }
		if (rakerColumns.equals(RakerColumns.NOV_3) && shift.equals(Shift.PM)) { return avail3pm; }

		if (rakerColumns.equals(RakerColumns.NOV_9) && shift.equals(Shift.AM)) { return avail9am; }
		if (rakerColumns.equals(RakerColumns.NOV_9) && shift.equals(Shift.PM)) { return avail9pm; }
		if (rakerColumns.equals(RakerColumns.NOV_10) && shift.equals(Shift.AM)) { return avail10am; }
		if (rakerColumns.equals(RakerColumns.NOV_10) && shift.equals(Shift.PM)) { return avail10pm; }

		// default
		System.out.println(dF + "in getAssignment - hit the default case");
		System.exit(1);
		return null;
	}


	/*
	 * returns team assignment given and day and shift
	 * or -1 if not assigned
	 */
	public Team getAssignment(RakerColumns rakerColumns, Shift shift) {

		if (rakerColumns.equals(RakerColumns.NOV_2) && shift.equals(Shift.AM)) { return assign2am; }
		if (rakerColumns.equals(RakerColumns.NOV_2) && shift.equals(Shift.PM)) { return assign2pm; }
		if (rakerColumns.equals(RakerColumns.NOV_3) && shift.equals(Shift.AM)) { return assign3am; }
		if (rakerColumns.equals(RakerColumns.NOV_3) && shift.equals(Shift.PM)) { return assign3pm; }

		if (rakerColumns.equals(RakerColumns.NOV_9) && shift.equals(Shift.AM)) { return assign9am; }
		if (rakerColumns.equals(RakerColumns.NOV_9) && shift.equals(Shift.PM)) { return assign9pm; }
		if (rakerColumns.equals(RakerColumns.NOV_10) && shift.equals(Shift.AM)) { return assign10am; }
		if (rakerColumns.equals(RakerColumns.NOV_10) && shift.equals(Shift.PM)) { return assign10pm; }

		// default
		System.out.println(dF + "in getAssignment - hit the default case");
		System.exit(1);
		return null;
	}

	/*
	 * stringToBoolean
	 * converts "1", "", "0"  to TRUE, TRUE, FALSE
	 */
	private Boolean stringToBoolean(String theString) {

		if (theString.equals("1")) { return new Boolean(true); }
		if (theString.equals(""))  { return new Boolean(true); }
		if (theString.equals("0")) { return new Boolean(false); }

		System.out.println(dF + "in stringToBoolean - cannot convert "
				+ theString 
				+ " to a boolean value");
		System.exit(1);
		return null;
	}

	/*
	 * toStringCommaDelimited
	 * return fields as a comma-delimited string
	 */
	public String toStringCommaDelimitedAllFields() {
		String s = new String();

		s = getFirst() 
				+ "," + getLast() 
				+ "," 
				+ ",";

		// availability by day/shift
		for (RakerColumns rakerColumns : RakerColumns.DAYS) {
			for (Shift shift : Shift.SHIFTS) {
				s = s + getAvailability(rakerColumns, shift).toString();
			}
		}
		// assignment by day/shift
		for (RakerColumns rakerColumns : RakerColumns.DAYS) {
			for (Shift shift : Shift.SHIFTS) {
				s = s + getAssignment(rakerColumns, shift).toString();
			}
		}
		return s;
	}
}
